package shuzu;

public class _01顺时针打印二维数组 {
    /**
     * 顺时针打印二维数组
     输入
     1 	2 	3 	4
     5 	6 	7 	8
     9 	10 	11 	12
     13	14	15	16
     输出
     1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
     */
    public static void main(String[] args) {
        int[][] matrix = {
                {1, 2, 3, 4, 100},
                {5, 6, 7, 8, 101},
                {9, 10, 11, 12, 102},
                {13, 14, 15, 16, 103},
                {104, 105, 106, 107, 103},
        };
        print(matrix);
    }

    private static void print(int[][] matrix) {
        int leftUpRow=0,leftUpCol=0,rightDownRow=matrix.length-1,rightDownCol=matrix[0].length-1;
        while (leftUpRow <= rightDownRow && leftUpCol <= rightDownCol) {
            int r = leftUpRow, c = leftUpCol;
            while (c <= rightDownCol) {
                System.out.print(matrix[r][c++] + " ");
            }
            c = rightDownCol;
            r++;
            while (r <= rightDownRow) {
                System.out.print(matrix[r++][c] + " ");
            }
            r = rightDownRow;
            c--;
            while (c >= leftUpCol) {
                System.out.print(matrix[r][c--] + " ");
            }
            c = leftUpCol;
            r--;
            while (r > leftUpRow) {
                System.out.print(matrix[r--][c] + " ");
            }
            leftUpCol++;
            leftUpRow++;
            rightDownRow--;
            rightDownCol--;
        }
    }
}
